package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.pojos.DCategory;
import com.jilefojing.beans.query.DCategoryQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IDCategoryDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IDCategoryMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月26日 16时31分05秒
 */
@Repository
public class DCategoryDao extends BaseDao<DCategory> implements IDCategoryDao {
	@Autowired
	private IDCategoryMapper dCategoryMapper;

	@Override
	public Pagination<DCategory> getPagination(DCategoryQuery dCategoryQuery, Pagination<DCategory> pagination) {
		Example example = new Example(DCategory.class);
		Criteria criteria = example.createCriteria();
		if (Detect.notEmpty(dCategoryQuery.getIds())) {
			criteria.andIn(DCategory.FIELD_ID, dCategoryQuery.getIds());
		}

		if (Detect.notEmpty(dCategoryQuery.getParentIds())) {
			criteria.andIn(DCategory.FIELD_PARENTID, dCategoryQuery.getParentIds());
		}

		if (dCategoryQuery.getEntity() != null) {
			if (dCategoryQuery.getEntity()) {
				criteria.andEqualTo(DCategory.FIELD_ENTITY, 1);
			} else {
				criteria.andEqualTo(DCategory.FIELD_ENTITY, 0);
			}
		}
		if (Detect.notEmpty(dCategoryQuery.getCreateUserIds())) {
			criteria.andIn(DCategory.FIELD_CREATEUSER, dCategoryQuery.getCreateUserIds());
		}
		if (Detect.notEmpty(dCategoryQuery.getItemTypes())) {
			List<Short> itemTypeCodes = new ArrayList<Short>();
			for (ItemTypeEnumForSource _iteType : dCategoryQuery.getItemTypes()) {
				itemTypeCodes.add(_iteType.getCode());
			}

			criteria.andIn(DCategory.FIELD_ITEMTYPE, itemTypeCodes);
		}
		if (Detect.notEmpty(dCategoryQuery.getLike_name())) {
			criteria.andLike(DCategory.FIELD_NAME, "%" + dCategoryQuery.getLike_name() + "%");
		}
		if (Detect.notEmpty(dCategoryQuery.getName())) {
			criteria.andEqualTo(DCategory.FIELD_NAME, dCategoryQuery.getName());
		}

		if (dCategoryQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(DCategory.FIELD_CREATETIME, dCategoryQuery.getEqualCreateTime());
		}

		if (dCategoryQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(DCategory.FIELD_CREATETIME, dCategoryQuery.getEqualAndBeforCreateTime());
		}

		if (dCategoryQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(DCategory.FIELD_CREATETIME, dCategoryQuery.getEqualAndAfterCreateTime());
		}

		if (dCategoryQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(DCategory.FIELD_CREATETIME, dCategoryQuery.getBeforCreateTime());
		}

		if (dCategoryQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(DCategory.FIELD_CREATETIME, dCategoryQuery.getAfterCreateTime());
		}

		if (dCategoryQuery.getEqualUpdateTime() != null) {
			criteria.andEqualTo(DCategory.FIELD_UPDATETIME, dCategoryQuery.getEqualUpdateTime());
		}

		if (dCategoryQuery.getEqualAndBeforUpdateTime() != null) {
			criteria.andGreaterThanOrEqualTo(DCategory.FIELD_UPDATETIME, dCategoryQuery.getEqualAndBeforUpdateTime());
		}

		if (dCategoryQuery.getEqualAndAfterUpdateTime() != null) {
			criteria.andLessThanOrEqualTo(DCategory.FIELD_UPDATETIME, dCategoryQuery.getEqualAndAfterUpdateTime());
		}

		if (dCategoryQuery.getBeforUpdateTime() != null) {
			criteria.andGreaterThan(DCategory.FIELD_UPDATETIME, dCategoryQuery.getBeforUpdateTime());
		}

		if (dCategoryQuery.getAfterUpdateTime() != null) {
			criteria.andLessThan(DCategory.FIELD_UPDATETIME, dCategoryQuery.getAfterUpdateTime());
		}

		if (Detect.notEmpty(dCategoryQuery.getCkeys())) {
			criteria.andIn(DCategory.FIELD_CKEY, dCategoryQuery.getCkeys());
		}

		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}
		return super.getPagination(pagination, example);
	}

	@Override
	public List<DCategory> getListByIds(List<Long> ids) {
		Example example = new Example(DCategory.class);
		example.createCriteria().andIn(DCategory.FIELD_ID, ids);
		return super.getListByExample(example);
	}

	@Override
	public List<DCategory> getListByItemCategory(String ckey, String name, ItemTypeEnumForSource itemType) {
		Example example = new Example(DCategory.class);
		example.createCriteria().andEqualTo(DCategory.FIELD_CKEY, ckey).andEqualTo(DCategory.FIELD_NAME, name).andEqualTo(DCategory.FIELD_ITEMTYPE, itemType.getCode());
		return super.getListByExample(example);
	}

	@Override
	public List<DCategory> getListByItemCkey(String ckey, ItemTypeEnumForSource itemType) {
		Example example = new Example(DCategory.class);
		example.createCriteria().andEqualTo(DCategory.FIELD_CKEY, ckey).andEqualTo(DCategory.FIELD_ITEMTYPE, itemType.getCode());
		return super.getListByExample(example);
	}

	@Override
	public List<DCategory> getListByItemNameForNotEntity(String name, ItemTypeEnumForSource itemType) {
		Example example = new Example(DCategory.class);
		example.createCriteria().andEqualTo(DCategory.FIELD_NAME, name).andEqualTo(DCategory.FIELD_ITEMTYPE, itemType.getCode()).andEqualTo(DCategory.FIELD_ENTITY, 0);
		return super.getListByExample(example);
	}

	@Override
	public int countByParentId(long parentId) {
		return this.countByParentIds(Arrays.asList(new Long[] { parentId }));
	}

	@Override
	public int countByParentIds(List<Long> parentIds) {
		Example example = new Example(DCategory.class);
		example.createCriteria().andIn(DCategory.FIELD_PARENTID, parentIds);
		return super.countByExample(example);
	}
}
